function ajaxRequest()
{
	 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE
	 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
	  for (var i=0; i<activexmodes.length; i++){
	   try{
	    return new ActiveXObject(activexmodes[i]);
	   }
	   catch(e){
	    //suppress error
	   }
	  }
	 }
	 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
	  return new XMLHttpRequest();
	 else
	  return false;
}

function new_post(title, content)
{
	var mypostrequest=new ajaxRequest();
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
	  if (mypostrequest.status==200){
		var url = mypostrequest.responseText;
	    window.location.href = url;
	  }
	  else{
	   alert("An error has occured making the request. Operation Failed.")
	  }
	 }
	};
	title=encodeURIComponent(title);
	content=encodeURIComponent(content);
	var parameters="title="+title+"&content="+content;
	mypostrequest.open("POST", "/post/new.html", true);
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mypostrequest.send(parameters);    
}

function delete_post(pid)
{
	var mypostrequest=new ajaxRequest();
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
	  if (mypostrequest.status==200){
		  window.location.href = '/post';
	  }
	  else{
	   alert("An error has occured during delete post. Operation Failed. status: "+mypostrequest.status);
	  }
	 }
	};
	var parameters="action=delete";
	mypostrequest.open("POST", "/post/"+pid+".html", true);
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mypostrequest.send(parameters);  
}

function update_post(title, content, pid){
	var mypostrequest=new ajaxRequest();
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
	  if (mypostrequest.status==200){
	    window.location.href = '/post/'+pid+'.html';
	  }
	  else{
	   alert("An error has occured making the request. Operation Failed.");
	  }
	 }
	};
	title=encodeURIComponent(title);
	content=encodeURIComponent(content);
	pid=encodeURIComponent(pid);
	var parameters="action=update&title="+title+"&content="+content;
	mypostrequest.open("POST", "/post/"+pid+".html", true);
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mypostrequest.send(parameters);
}
